using I2.Loc;
using UnityEngine;

public class JetpackBehavior : MonoBehaviour
{
	public bool activated;

	public bool particles;

	public bool isFlying;

	[HideInInspector]
	public SaveFloat fuel;

	public GameObject reactiveFire;

	public ThirdPersonController cacheController;

	public MeshRenderer meshJetpack;

	private ParticleSystem[] _particleSystems;

	public bool isMine
	{
		get
		{
			if (cacheController == null)
			{
				return false;
			}
			return cacheController.pBehavior.isMine;
		}
	}

	private void Awake()
	{
		_particleSystems = GetComponentsInChildren<ParticleSystem>();
		meshJetpack = GetComponent<MeshRenderer>();
		fuel = 0f;
		if (WalkWindow.instance != null && (isMine || settings.offlineMode))
		{
			SetJumpButton(false);
		}
	}

	public void ActivateParticles()
	{
		particles = true;
		if (_particleSystems != null)
		{
			for (int i = 0; i < _particleSystems.Length; i++)
			{
				_particleSystems[i].Stop();
				_particleSystems[i].Play();
			}
		}
	}

	public void ValidateFuel()
	{
		if (isMine)
		{
			settings.jetpackFuel = fuel.GetValue();
		}
	}

	public void DeactivateParticles()
	{
		particles = false;
		for (int i = 0; i < _particleSystems.Length; i++)
		{
			_particleSystems[i].Stop();
		}
	}

	public void Init()
	{
		if (CanBeActivated())
		{
			Activate();
			if (isMine)
			{
				SetJumpButton(true);
			}
		}
		else
		{
			Deactivate(true);
			if (isMine)
			{
				SetJumpButton(false);
			}
		}
	}

	public bool CanBeActivated()
	{
		return fuel.GetValue() > 0f;
	}

	public static void ActivateNewBalloon()
	{
		settings.jetpackFuel = 100f;
		if (GameController.instance != null)
		{
			GameController.instance.playerScript.tController.jetpack.fuel = settings.jetpackFuel;
			GameController.instance.playerScript.tController.jetpack.CanBeActivated();
			GameController.instance.playerScript.ActivateJetpack();
		}
		if (GameController.instance != null)
		{
			GameController.instance.playerScript.tController.jetpack.Init();
		}
	}

	public bool Activate()
	{
		if ((fuel.GetValue() > 0f && isMine && PlayerBehavior.MyPlayer != null && PlayerBehavior.MyPlayer.enableJetpackUse) || !isMine)
		{
			if (isMine)
			{
				SetJumpButton(true);
			}
			activated = true;
			base.gameObject.SetActive(true);
			return true;
		}
		return false;
	}

	public void BroadcastParticle()
	{
		if (isFlying)
		{
			reactiveFire.SetActive(true);
		}
		else
		{
			reactiveFire.SetActive(false);
		}
	}

	public void DeactivateFromDisconnect()
	{
		activated = false;
		base.gameObject.SetActive(false);
		SetJumpButton(false);
		CheckCache();
	}

	public void Deactivate(bool fromStart = false)
	{
		bool flag = fuel.GetValue() <= 0f;
		if (activated && !fromStart && flag)
		{
			GameTopWindow.instance.ShowMessages(ScriptLocalization.Get("Game_Common/no_fuel"));
		}
		activated = false;
		isFlying = false;
		if (flag)
		{
			base.gameObject.SetActive(false);
			SetJumpButton(false);
		}
		else if (!PlayerBehavior.MyPlayer.enableJetpackUse)
		{
			SetJumpButton(false);
		}
		else
		{
			reactiveFire.SetActive(false);
			DeactivateParticles();
		}
		CheckCache();
		settings.jetpackFuel = fuel.GetValue();
		Save.SaveFloat("fuel", settings.jetpackFuel);
	}

	public void DeactivateForPhoton()
	{
		activated = false;
		reactiveFire.SetActive(false);
		base.gameObject.SetActive(false);
	}

	private bool OnLadder()
	{
		return GameController.instance.curScriptLestnica != null;
	}

	private void DisableLadder()
	{
		if (GameController.instance.curScriptLestnica != null)
		{
			GameController.instance.curScriptLestnica.OnStair(false);
		}
	}

	private void CheckCache()
	{
		if (cacheController == null)
		{
			cacheController = base.transform.GetComponentInParent<ThirdPersonController>();
		}
	}

	private void SetJumpButton(bool jetpack)
	{
		if (WalkWindow.instance.jumpButton != null && WalkWindow.instance.jetpackButton != null)
		{
			if (jetpack)
			{
				WalkWindow.instance.jumpButton.gameObject.SetActive(false);
				WalkWindow.instance.jetpackButton.gameObject.SetActive(true);
			}
			else
			{
				WalkWindow.instance.jumpButton.gameObject.SetActive(true);
				WalkWindow.instance.jetpackButton.gameObject.SetActive(false);
			}
		}
		else
		{
			BCWDebug.LogWarning("jumpButton is null!");
		}
	}

	private void Update()
	{
		if (!isMine)
		{
			return;
		}
		if (GameController.instance.playerScript.isDead || GameController.instance.playerScript.isShock)
		{
			Deactivate();
			GameController.instance.playerScript.DeactivateJetpack();
		}
		if (!activated)
		{
			return;
		}
		CheckCache();
		if (isFlying)
		{
			if (fuel.GetValue() > 0f && PlayerBehavior.MyPlayer.enableJetpackUse)
			{
				if (OnLadder())
				{
					DisableLadder();
				}
				fuel = fuel.GetValue() - Time.deltaTime * 5f / GameController.instance.playerScript.GetJetpackFuelMultiplier();
				if (!reactiveFire.activeSelf)
				{
					ActivateParticles();
					reactiveFire.SetActive(true);
				}
			}
			else
			{
				Deactivate();
				GameController.instance.playerScript.DeactivateJetpack();
			}
		}
		else
		{
			if (reactiveFire.activeSelf)
			{
				reactiveFire.SetActive(false);
			}
			particles = false;
		}
	}

	public void ActivateMeshJetpack(bool val)
	{
		meshJetpack.enabled = val;
	}
}
